﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIPO.Library.DataAccess.Transaction;
using SIPO.Library.DomainObject.Transaction;
using System.Configuration;
using System.Data;
using Microsoft.Reporting.WebForms;
using SIPO.Web.Support;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;


namespace SIPO.Web.Reports.Transaction
{
    public partial class rptHistoryTransactionViewer : BasePage
    {
        recInvoiceRepo recInvoiceDal = new recInvoiceRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        //   rptInvoiceRepo rptInvoicedal = new rptInvoiceRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {

                DateTime? dPeriodeAwal = Context.Request.Params["periodeAwal"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["PeriodeAwal"], Helper.Culture.Indonesian);
                DateTime? dPeriodeAkhir = Context.Request.Params["periodeAkhir"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["PeriodeAkhir"], Helper.Culture.Indonesian);
                string sPeriodeAwal = string.Format("{0:yyyyMMdd}", dPeriodeAwal);
                string sPeriodeAkhir = string.Format("{0:yyyyMMdd}", dPeriodeAkhir);
                string parameters = string.Empty;
                string parametersPay = string.Empty;
                string parametersInvoiceNoBill = string.Empty;
                string parametersInvoiceNoPayment = string.Empty;
                if (!dPeriodeAwal.Equals(null) && !dPeriodeAkhir.Equals(null))
                {
                    if (base.DebtorID != (int?)null)
                    {

                        recDebtorSectorRepo recDebtorSectorDal = new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                        recSectorRegistrationRepo recSectorRegistrationDal = new recSectorRegistrationRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                        List<recDebtorSector> debtorsectors = recDebtorSectorDal.GetById(base.DebtorID.Value, base.SubSubSectorIDs);
                        List<recSectorRegistration> sectorRegistrations = recSectorRegistrationDal.GetList(base.RegistrationID, base.DebtorID.Value, base.SubSubSectorIDs);
                        int i = 0;
                        string DSid = string.Empty;
                        foreach (recDebtorSector DS in debtorsectors)
                        {
                            if (i == 0)
                                DSid += DS.DebtorSectorID;
                            else
                                DSid += "," + DS.DebtorSectorID;
                            i++;
                        }
                        int j = 0;
                        string invoiceNos = string.Empty;
                        foreach (recSectorRegistration sectorRegistration in sectorRegistrations)
                        {
                            if (j == 0)
                                invoiceNos += "'" + sectorRegistration.InvoiceNo + "'";
                            else
                                invoiceNos += "," + "'" + sectorRegistration.InvoiceNo.ToString() + "'";
                            j++;
                        }
                        parameters += "where transactions.recBilling.BillingDate BETWEEN ISNULL ('" + sPeriodeAwal + "', recBilling.BillingDate) AND ISNULL ('" + sPeriodeAkhir + "', recBilling.BillingDate)  and transactions.recBilling.debtorsectorid in(" + DSid + ")";
                        parametersPay += "where transactions.recPayment.paymentDate BETWEEN ISNULL ('" + sPeriodeAwal + "', recPayment.paymentDate) AND ISNULL ('" + sPeriodeAkhir + "', recPayment.paymentDate)  and transactions.recPayment.debtorsectorid in(" + DSid + ")";
                        if (sectorRegistrations.Count > 0)
                        {
                            parametersInvoiceNoBill += "where transactions.recBilling.BillingDate BETWEEN ISNULL ('" + sPeriodeAwal + "', recBilling.BillingDate) AND ISNULL ('" + sPeriodeAkhir + "', recBilling.BillingDate)  and transactions.recBilling.InvoiceNo in (" + invoiceNos + ")";
                            parametersInvoiceNoPayment += "where transactions.recPayment.paymentDate BETWEEN ISNULL ('" + sPeriodeAwal + "', recPayment.paymentDate) AND ISNULL ('" + sPeriodeAkhir + "', recPayment.paymentDate)  and transactions.recPayment.InvoiceNo in (" + invoiceNos + ")";
                        }
                        else
                        {
                            invoiceNos += "'0'";
                            parametersInvoiceNoBill += "where transactions.recBilling.BillingDate BETWEEN ISNULL ('" + sPeriodeAwal + "', recBilling.BillingDate) AND ISNULL ('" + sPeriodeAkhir + "', recBilling.BillingDate)  and transactions.recBilling.InvoiceNo in (" + invoiceNos + ")";
                            parametersInvoiceNoPayment += "where transactions.recPayment.paymentDate BETWEEN ISNULL ('" + sPeriodeAwal + "', recPayment.paymentDate) AND ISNULL ('" + sPeriodeAkhir + "', recPayment.paymentDate)  and transactions.recPayment.InvoiceNo in (" + invoiceNos + ")";
                        }
                    }
                    else
                    {
                        parameters += "where transactions.recBilling.BillingDate BETWEEN ISNULL ('" + sPeriodeAwal + "', recBilling.BillingDate) AND ISNULL ('" + sPeriodeAkhir + "', recBilling.BillingDate)";
                        parametersPay += "where transactions.recPayment.PaymentDate BETWEEN ISNULL ('" + sPeriodeAwal + "', recPayment.PaymentDate) AND ISNULL ('" + sPeriodeAkhir + "', recPayment.PaymentDate)";
                        parametersInvoiceNoBill += parameters;
                        parametersInvoiceNoPayment += parametersPay;

                    }
                }
                else
                {
                    if (base.DebtorID != (int?)null)
                    {

                        recDebtorSectorRepo recDebtorSectorDal = new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                        recSectorRegistrationRepo recSectorRegistrationDal = new recSectorRegistrationRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                        List<recDebtorSector> debtorsectors = recDebtorSectorDal.GetById(base.DebtorID.Value, base.SubSubSectorIDs);
                        List<recSectorRegistration> sectorRegistrations = recSectorRegistrationDal.GetList(base.RegistrationID, base.DebtorID.Value, base.SubSubSectorIDs);
                        int i = 0;
                        string DSid = string.Empty;
                        foreach (recDebtorSector DS in debtorsectors)
                        {
                            if (i == 0)
                                DSid += DS.DebtorSectorID;
                            else
                                DSid += "," + DS.DebtorSectorID;
                            i++;
                        }
                        int j = 0;
                        string invoiceNos = string.Empty;
                        foreach (recSectorRegistration sectorRegistration in sectorRegistrations)
                        {
                            if (j == 0)
                                invoiceNos += "'" + sectorRegistration.InvoiceNo + "'";
                            else
                                invoiceNos += "," + "'" + sectorRegistration.InvoiceNo.ToString() + "'";
                            j++;
                        }


                        parameters += "where transactions.recbilling.debtorsectorid in(" + DSid + ")";
                        parametersPay += "where transactions.recpayment.debtorsectorid in(" + DSid + ")";
                        if (sectorRegistrations.Count > 0)
                        {
                            parametersInvoiceNoBill = "where transactions.recBilling.InvoiceNo in (" + invoiceNos + ")";
                            parametersInvoiceNoPayment = "where transactions.recPayment.InvoiceNo in (" + invoiceNos + ")";
                        }
                        else
                        {
                            invoiceNos += "'0'";
                            parametersInvoiceNoBill = "where transactions.recBilling.InvoiceNo in (" + invoiceNos + ")";
                            parametersInvoiceNoPayment = "where transactions.recPayment.InvoiceNo in (" + invoiceNos + ")";
                        }
                    }
                }
                try
                {
                    // DataTable dt = rptInvoicedal.GetHistoryTransactions(parameters);
                    DataTable dt = recInvoiceDal.GetListHistoryReport(parameters, parametersPay, parametersInvoiceNoBill, parametersInvoiceNoPayment);
                    rsViewer.Reset();
                    rsViewer.LocalReport.ReportPath = Server.MapPath("~/reportviewer/Transaction/rptHistoryTransactions.rdlc");
                    ReportDataSource rds = new ReportDataSource();
                    rds.Name = "DataSet1";
                    rds.Value = dt;

                    rsViewer.LocalReport.DataSources.Clear();
                    rsViewer.LocalReport.DataSources.Add(rds);
                    rsViewer.LocalReport.Refresh();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }

            }
        }
    }
}